package com.xz.arzon.instance;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
 * Created by YLZX-Z0107 on 2014/7/16.
 */
public class ClientSingleton {
    static {
        System.out.println("HttpClient init");
        ConnectionSocketFactory plainsf = new PlainConnectionSocketFactory();
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
        cm.setMaxTotal(10);
        HttpClients.custom().setConnectionManager(cm).build();
    }

    private ClientSingleton() {
        System.out.println("ClientFactory create");
    }

    private static class SingletonHolder {
        private static ClientSingleton instance = new ClientSingleton();
    }

    public static ClientSingleton getInstance() {
        return SingletonHolder.instance;
    }

    public CloseableHttpClient getClient() {
        return HttpClients.createDefault();
    }
}
